home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
ISSUE03
/
CLIPBRD
/
BDAY.PAS
next >
Wrap
Pascal/Delphi Source File
|
1995-06-06
|
3KB
|
106 lines
unit Bday;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ClipBrd;
type
TPersonRec = record
Name: string[100];
Age: integer;
BirthDate: TDateTime;
end;
TBirthDay = class(TComponent)
private
{ Protected declarations }
FPersonRec: TPersonRec;
public
procedure CopyToClipBoard;
procedure PasteFromClipBoard;
property Person: TPersonRec read FPersonRec write FPersonRec;
end;
var
CF_BIRTHDAY: word;
procedure Register;
implementation
procedure TBirthday.CopyToClipBoard;
const
CRLF = #13#10; { Carriage return line feed }
FormatText = 'Name: %s%sAge: %d%sBirthDate: ';
var
Data: THandle;
DataPtr: Pointer;
Len: Integer;
TempStr: string;
begin
{ Allocate memory from global heap }
Data := GlobalAlloc(GMEM_MOVEABLE, SizeOf(FPersonRec));
try
DataPtr := GlobalLock(Data); { Get a pointer to the memory area }
try
{ Move the data in Buffer to DataPtr }
Move(FPersonRec, DataPtr^, SizeOf(FPersonRec));
ClipBoard.Open; { This is only required if multiple clipboard formats are }
{ being saved at once. Otherwise, if only one format it being}
{ sent to the clipboard, don't call it. }
try
ClipBoard.SetAsHandle(CF_BIRTHDAY, Data);
{ Now copy also in the CF_TEXT format also }
with FPersonRec do
TempStr := Format(FormatText, [Name, CRLF, Age, CRLF])+
DateToStr(BirthDate);
ClipBoard.AsText := TempStr;
finally
Clipboard.Close; { Only call this if you previously called Clipboard.Open() }
end
finally
GlobalUnlock(Data); { Unlock the globally allocated memory }
end;
except
GlobalFree(Data); { Free the memory allocated, only if }
raise; { an exception occurs as this memory is }
end; { managed by Windows. }
end;
procedure TBirthday.PasteFromClipBoard;
var
Data: THandle;
DataPtr: Pointer;
C: Char;
Size: Integer;
begin
Data := ClipBoard.GetAsHandle(CF_BIRTHDAY); { Get the data on the clipboard }
try
if Data = 0 then Exit; { Exit is unsuccessful }
DataPtr := GlobalLock(Data); { Lock the Global memory object }
try
if SizeOf(FPersonRec) > GlobalSize(Data) then Size := GlobalSize(Data);
{ Copy contents of DataPtr to Buffer }
Move(DataPtr^, FPersonRec, SizeOf(FPersonRec));
finally
GlobalUnlock(Data); { Unlock the global memory object }
end;
except
GlobalFree(Data); { Free the memory allocated, only if }
raise; { an exception occurs as this memory is }
end; { managed by the Windows }
end;
procedure Register;
begin
RegisterComponents('Test', [TBirthday]);
end;
initialization
{ Register the special clipboard format CF_BIRTHDAY}
CF_BIRTHDAY := RegisterClipBoardFormat('CF_BIRTHDAY');
end.